package com.htjx.mall.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;

import com.htjx.mall.commons.mappers.SpecificationMapper;
import com.htjx.mall.commons.models.enums.SeqType;
import com.htjx.mall.commons.models.product.Specification;

@Service
public class SpecificationService {
	@Autowired
	private SpecificationMapper specificationMapper;
	@Autowired
	private SequenceManager sequenceManager;
	@Autowired
	private TransactionTemplate transactionTemplate;
	
	public void insertOne(final Specification specification) {
		long[] seq = sequenceManager.buildBatchSeq(SeqType.Specification, 1);
		specification.setId(seq[0]);
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				specificationMapper.insertOne(specification);
			}
		});
	}
	
	public Specification selectById(long id) {
		return specificationMapper.selectOne(id);
	}
	
	public void updateOne(final Specification specification) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				specificationMapper.updateOne(specification);
			}
		});
	}
	
	public void deleteOne(final long id) {
		transactionTemplate.execute(new TransactionCallbackWithoutResult() {
			@Override
			protected void doInTransactionWithoutResult(TransactionStatus status) {
				specificationMapper.deleteOne(id);
			}
		});
	}
	
	public List<Specification> SelectByCatalogId(long catalogId) {
		return specificationMapper.selectByCatalogId(catalogId);
	}
}
